﻿using System.Media;
using System.Speech.Synthesis;
using System.IO;
using System.Runtime.InteropServices;
using System.Speech.AudioFormat;
using System.Collections.Generic;

namespace _180FrameWork.Common
{
    public static class SoundHelper
    { 
        #region TTS
        private static SpeechSynthesizer speaker = new SpeechSynthesizer();
        /// <summary>
        /// 获取所有语音
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetVoices() {
            IList<string> voiceList = new List<string>();
            foreach (var iv in speaker.GetInstalledVoices())
            {
                voiceList.Add(iv.VoiceInfo.Name);
            }
            return voiceList;
        }
        /// <summary>
        /// 设置语音
        /// </summary>
        /// <param name="SpeakerName"></param>
        public static void SetVoice(string SpeakerName)
        {            
            foreach (var iv in speaker.GetInstalledVoices())
            {
                if (iv.VoiceInfo.Name == SpeakerName)
                {
                    iv.Enabled = true;
                    speaker.SelectVoice(SpeakerName);
                    break;
                }
            }            
        }
        /// <summary>
        /// 转换成语音文件
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Stream ToStreamWav(string text)
        {
            MemoryStream stramAudio = new MemoryStream();
            speaker.SetOutputToWaveStream(stramAudio);
            speaker.Speak(text);
            speaker.SetOutputToNull();
            return stramAudio;
        }

        /// <summary>
        /// 语音播放
        /// </summary>
        /// <param name="text">语音的内容</param>
        public static bool Speak(string text)
        {
            Prompt prompt = speaker.SpeakAsync(text);
            bool isCompleted = false;
            while (true)
            {
                if (prompt.IsCompleted)
                {
                    isCompleted = true;
                    break;
                }
            }
            return isCompleted;
        }

        /// <summary>
        /// 语音播放
        /// </summary>
        /// <param name="text">语音的内容</param>
        public static void SpeakAsync(string text)
        {
            speaker.SpeakAsync(text);
        }
        /// <summary>
        /// 设置语速(-10到10)
        /// </summary>
        /// <param name="Rate"></param>
        public static void SetRate(int Rate)
        {
            speaker.Rate = Rate;
        }
        /// <summary>
        /// 设置音量(0-100)
        /// </summary>
        /// <param name="Volume"></param>
        public static void SetVolume(int Volume)
        {
            speaker.Volume = Volume;
        }
        #endregion

        #region 播放声音文件
        private static SoundPlayer player = new SoundPlayer();
        /// <summary>
        /// 播放声音文件
        /// </summary>
        /// <param name="s"></param>
        public static void Sound(Stream s)
        {
            player.Stream = s;
            player.PlaySync();
        }
        #endregion

        #region 嗡鸣
        [DllImport("kernel32.dll", EntryPoint = "Beep")]
        private static extern int Beep(int dwFreq, int dwDuration);
        [DllImport("user32.dll")]
        private static extern bool MessageBeep(BeepType beepType);
        /// <summary>
        /// 
        /// </summary>
        public static void Beep()
        {          
            try
            {
                Beep(600,200);
            }
            catch
            {
            }
        }
        #endregion
    }

    public enum BeepType
    {
        SimpleBeep = -1,
        IconAsterisk = 0x00000040,
        IconExclamation = 0x00000030,
        IconHand = 0x00000010,
        IconQuestion = 0x00000020,
        Ok = 0x00000000
    }
}
